/**
 * Make Bookmark Chrome browser extension
 *
 * This Google Chrome extension allows the user to create a bookmark
 * using a combination of the selected text and either the 'right clicked' link or
 * the page URL. The bookmark is copied to the clipboard.
 
 * Released under Apache License, Version 2.0.
 */

 /**
  * generic functions
  */
function copyTextToClipboard(id, textToCopy)
{
	//alert(textToCopy);
	
	document.body.innerHTML = '<p><a href="'+textToCopy+'" target="_blank" >'+textToCopy+'</a><form style="margin-top: -35px; margin-left: -500px;"><input type="text" id="' + id + '" value="'+textToCopy+'"></form></p>';
	document.getElementById("textToCopy").select();
	document.execCommand("Copy");
}

function removeRedirection(origLink)
{
	var k,x,t,i,j,p;
	
	t = origLink.replace(/[%]3A/ig,':').replace(/[%]2f/ig,'/');
	i = t.lastIndexOf('http');
	if ( i > 0 ) {
		t = t.substring(i);
		
		j = t.indexOf('&');
		if ( j > 0 ) {
			t = t.substring(0,j);
		}
	}
	
	return t;
}

/**
 * Functions related to the right-clicked link.
 */

/**
 * Copy Link Text.
 */
function copy_LinkText(info, tab)
{
	//alert("'" + info.menuItemId + "' item:" + id);
	//alert("'" + info.pageUrl + "' item:" + id);
	alert("item:'" + id + "', page url:'" + info.pageUrl + "', url:'" + info.linkUrl + "', ");
}

/**
 * Copy Selected Text (usually Link Text) and Link Address (URL).
 */
function copy_SelText_LinkAddress(info, tab)
{
	var textToCopy = info.selectionText + " | " + info.linkUrl;
	
	copyTextToClipboard("textToCopy", textToCopy);
}

/**
 * Copy Link Address (URL) and Selected Text (usually Link Text).
 */
function copy_LinkAddress_SelText(info, tab)
{
	var textToCopy = info.linkUrl + " | " + info.selectionText;
	
	copyTextToClipboard("textToCopy", textToCopy);
}

/**
 * Copy Selected Text (usually Link Text) and Link Address (URL) after removing redirection from the URL.
 */
function copy_SelText_LinkAddress_NoRedirect(info, tab)
{
	var newURL = removeRedirection(info.linkUrl);
	var textToCopy = info.selectionText + " | " + newURL;
	copyTextToClipboard("textToCopy", textToCopy);
}

/**
 * Copy Link Address (URL) and Selected Text (usually Link Text) after removing redirection from the URL.
 */
function copy_LinkAddress_SelText_NoRedirect(info, tab)
{
	var newURL = removeRedirection(info.linkUrl);
	var textToCopy = newURL + " | " + info.selectionText;
	copyTextToClipboard("textToCopy", textToCopy);
}

// add a context menu to "link"
//chrome.contextMenus.create({"type": "normal", "title": "Copy Name", "contexts":["link"], "onclick": copy_LinkText, "id":"clt"});
//chrome.contextMenus.create({"type": "separator", "contexts":["link"]});
chrome.contextMenus.create({"type": "normal", "title": "Copy Selected Text + Link Address", "contexts":["link"], "onclick": copy_SelText_LinkAddress, "id":"cstla"});
chrome.contextMenus.create({"type": "normal", "title": "Copy Link Address + Selected Text", "contexts":["link"], "onclick": copy_LinkAddress_SelText, "id":"clast"});
chrome.contextMenus.create({"type": "separator", "contexts":["link"]});
chrome.contextMenus.create({"type": "normal", "title": "Copy Selected Text + Link Address (Remove redirection)", "contexts":["link"], "onclick": copy_SelText_LinkAddress_NoRedirect, "id":"cstlanr"});
chrome.contextMenus.create({"type": "normal", "title": "Copy Link Address + Selected Text (Remove redirection)", "contexts":["link"], "onclick": copy_LinkAddress_SelText_NoRedirect, "id":"clastnr"});
chrome.contextMenus.create({"type": "separator", "contexts":["link"]});

/**
 * Functions related to the selected text.
 */

/**
 * Copy Selected Text and Page Address
 */
function copy_SelText_PageAddress(info, tab)
{
	var textToCopy = info.selectionText + " | " + info.pageUrl;
	copyTextToClipboard("textToCopy", textToCopy);
}

// add a context menu to "selection"
chrome.contextMenus.create({"title": "Copy Selected Text + Page Address", "contexts":["selection"], "onclick": copy_SelText_PageAddress, "id":"cstpu"});

/* end */
